using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Markup;
using Common;

namespace MainView.Windows
{
	public partial class AutoStandardWindow : Window, IComponentConnector
	{
		public enum EAutoMode
		{
			Increase,
			Decrease
		}

		public static readonly DependencyProperty SelectedWellsNumProperty = DependencyProperty.Register("SelectedWellsNum", typeof(int), typeof(AutoStandardWindow), new UIPropertyMetadata(0));

		public static readonly DependencyProperty AutoModeProperty = DependencyProperty.Register("AutoMode", typeof(EAutoMode), typeof(AutoStandardWindow), new UIPropertyMetadata(EAutoMode.Increase));

		public int SelectedWellsNum
		{
			get
			{
				return (int)GetValue(SelectedWellsNumProperty);
			}
			set
			{
				SetValue(SelectedWellsNumProperty, value);
			}
		}

		public int CompoundTubeNum { get; set; }

		public decimal StartValue { get; set; }

		public EAutoMode AutoMode
		{
			get
			{
				return (EAutoMode)GetValue(AutoModeProperty);
			}
			set
			{
				SetValue(AutoModeProperty, value);
			}
		}

		public int Extent { get; set; }

		public AutoStandardWindow()
		{
			InitializeComponent();
			comboBox.ItemsSource = new List<KeyValuePair>
			{
				new KeyValuePair
				{
					Value = Utility.GetMessage("increase"),
					Key = EAutoMode.Increase
				},
				new KeyValuePair
				{
					Value = Utility.GetMessage("decrease"),
					Key = EAutoMode.Decrease
				}
			};
		}

		private void textBoxNum_Loaded(object sender, RoutedEventArgs e)
		{
			textBoxNum.Focus();
			textBoxNum.SelectAll();
		}

		private void btnOK_Click(object sender, RoutedEventArgs e)
		{
			CompoundTubeNum = GetValue(textBoxNum);
			if (CompoundTubeNum <= 0 || SelectedWellsNum % CompoundTubeNum != 0)
			{
				MessageBox.Show(Utility.GetMessage("msg_selectedwell_not_tube"), Utility.GetMessage("error"), MessageBoxButton.OK, MessageBoxImage.Hand);
				textBoxNum.Focus();
				textBoxNum.SelectAll();
				return;
			}
			StartValue = GetDecimalValue(textBoxStartValue);
			if (StartValue <= 0m)
			{
				MessageBox.Show(Utility.GetMessage("msg_concentration_not_valid"), Utility.GetMessage("error"), MessageBoxButton.OK, MessageBoxImage.Hand);
				textBoxStartValue.Focus();
				textBoxStartValue.SelectAll();
				return;
			}
			Extent = GetValue(textBoxExtent);
			if (Extent <= 0)
			{
				MessageBox.Show(Utility.GetMessage("msg_dilution_not_valid"), Utility.GetMessage("error"), MessageBoxButton.OK, MessageBoxImage.Hand);
				textBoxExtent.Focus();
				textBoxExtent.SelectAll();
			}
			else
			{
				base.DialogResult = true;
				Close();
			}
		}

		private int GetValue(TextBox tb)
		{
			int result = 0;
			int.TryParse(tb.Text, out result);
			if (result <= 0)
			{
				return -1;
			}
			return result;
		}

		private decimal GetDecimalValue(TextBox tb)
		{
			decimal result = 0m;
			decimal.TryParse(tb.Text, out result);
			if (result <= 0m)
			{
				return -1m;
			}
			return result;
		}
	}
}
